<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="部门名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="queryListData">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="depts" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="sn" label="部门编号" >
			</el-table-column>
			<el-table-column prop="name" label="部门名称" >
			</el-table-column>
			<el-table-column prop="state" label="部门状态" >
				<template scope="scope">
					<span style="color: #11b95c" v-if="scope.row.state===0">启用</span>
					<span style="color: red" v-else-if="scope.row.state===-1">禁用</span>
					<span style="color: plum" v-else>未知</span>
				</template>
			</el-table-column>
			<el-table-column prop="employee.username" label="部门经理">
			</el-table-column>
			<el-table-column prop="parent.name" label="上级部门" >
			</el-table-column>



			<el-table-column label="操作" >
				<template scope="scope">
                    <el-button type="primary" size="small" @click="handleDrawer(scope.$index, scope.row)" >部门员工</el-button>
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>



		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
			</el-pagination>
		</el-col>
		<!--抽屉页面-->
		<el-drawer
				title="我是1031吴俊彦"
				:visible.sync="drawer"
				direction="rtl"
				size="65%">
            <!--高级查询-->
			<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
				<el-form :inline="true" :model="empfilters">
					<el-form-item>
						<el-input v-model="empfilters.keyname" placeholder="姓名"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" v-on:click="queryDrawer">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="empAdd">新增</el-button>
					</el-form-item>
				</el-form>
			</el-col>
			<el-table :data="gridData" @selection-change="selsChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column type="index" width="60">
                </el-table-column>
				<el-table-column property="username" label="姓名" width="150"></el-table-column>
				<el-table-column property="phone" label="电话" width="200"></el-table-column>
				<el-table-column property="age" label="年龄"></el-table-column>
				<el-table-column label="操作" >
					<template scope="escope">
						<el-button size="small" @click="empEdit(escope.$index, escope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="empDel(escope.$index, escope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!--抽屉分页批量删除-->
			<el-col :span="24" class="toolbar">
				<el-button type="danger" @click="empRemove" :disabled="this.sels.length===0">批量删除</el-button>
				<el-pagination layout="prev, pager, next" @current-change="eCurrentChange" :page-size="epageSize" :total="etotal" style="float:right;">
				</el-pagination>
			</el-col>
		</el-drawer>

		<!--操作界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="部门名称" prop="name">
					<el-input v-model="addForm.name" ></el-input>
				</el-form-item>
				<el-form-item label="部门编号">
					<el-input v-model="addForm.sn" ></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="addForm.state">
						<el-radio class="radio" :label="0">启用</el-radio>
						<el-radio class="radio" :label="-1">禁用</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="部门经理">
					<el-select v-model="addForm.employee" value-key="id" clearable placeholder="请选择">
						<el-option
								v-for="item in emps"
								:key="item.id"
								:label="item.username"
								:value="item">
							<span style="float: left">{{ item.username }}</span>
							<span style="float: right; color:#ff00ff; font-size: 13px">{{ item.phone }}</span>
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="上级部门">
					<el-cascader
							v-model="addForm.parent"
							:options="deptTree"
							:props="{ checkStrictly: true}"
							clearable></el-cascader>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
		<!--抽屉的操作页面-->
		<el-dialog :etitle="title" :visible.sync="eeditFormVisible" :close-on-click-modal="false">
			<el-form :model="eaddForm" label-width="80px"  ref="eaddForm">
				<el-form-item label="姓名" prop="name">
					<el-input v-model="eaddForm.username" ></el-input>
				</el-form-item>
				<el-form-item label="邮箱">
					<el-input v-model="eaddForm.email" ></el-input>
				</el-form-item>
				<el-form-item label="电话">
					<el-input v-model="eaddForm.phone" ></el-input>
				</el-form-item>
				<el-form-item label="状态">
					<el-radio-group v-model="eaddForm.state">
						<el-radio class="radio" :label="0">离职</el-radio>
						<el-radio class="radio" :label="1">在职</el-radio>
					</el-radio-group>
				</el-form-item>
				<el-form-item label="年龄">
					<el-input v-model="eaddForm.age" ></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="eaddSubmit" :loading="eaddLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
				emps:[],// 所有的部门经理
                drawer: false,// 默认关闭抽屉
                gridData:[],// 抽屉数据
				deptId:"",
				filters: {
					keyword: ''
				},
                empfilters:{
                    keyname:''
				},
                title:'新增',// 默认模态框标题为新增
                etitle:'新增',// 默认模态框标题为新增
                depts: [],// 列表内容
                deptTree:[],// 上级部门
				total: 0,// 当前页
                etotal:0,
				page: 1,
                pageSize:5,// 每页条数
                epageSize:3,// 每页条数
                currentPage:1,// 默认第一页
                ecurrentPage:1,// 默认第一页
				listLoading: false,
				sels: [],//列表选中列
                editFormVisible:false,
                eeditFormVisible:false,
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				//编辑界面数据
                addForm:{//双向绑定的对象
                    id:null,
                    name:'',
                    sn:'',
                    state:0,
                    parent:null,
                    employee:null
                },
                eaddForm:{
				    id:null,
				    username:'',
				    email:'',
				    phone:'',
				    password:'',
				    age:'',
				    state:0,
                    department_id:''
				},
				addLoading: false,
				eaddLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				eaddFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},

			}
		},
		methods: {
		    /*分页*/
			handleCurrentChange(page) {

                this.currentPage=page;
				this.loadListData();
			},
			//获取用户列表
			loadListData() {
				let para = {
                    currentPage: this.currentPage,
                    pageSize:5,
					keyword: this.filters.keyword,
				}
				this.$http.post("/dept",para).then(res=>{
                    /*res是返回的数据*/
                    this.depts=res.data.bags;
				    this.total=res.data.total;
				})
			},
			/*高级查询*/
            queryListData(){
                currentPage: this.currentPage=1;
                this.loadListData();
			},
			//删除
			handleDel(i, row) {
                this.$confirm('亲，确认删除数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                let id = row.id;
                this.$http.delete("/dept/"+id).then(res=>{
                    if(res.data.success){
                        this.$message({
                            type:'success',
                            message:res.data.message
						});
					}else {
                        this.$message({
                            type: 'error',
                            message: res.data.message
                        });
					}
					// 刷新列表
					this.loadListData();
				})
                }).catch(()=>{
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })

			},
			//显示编辑界面
			handleEdit(i, row) {
			    /*加载级联数据*/
			    this.loadListDeptTree();
			    /*设置表标题*/
            	this.title='编辑';
            	/*显示模态框*/
				this.editFormVisible = true;
				/*设置回显*/
                let para=Object.assign(this.addForm, row);
			},
			//显示新增界面
			handleAdd() {
                /*加载级联数据*/
                this.loadListDeptTree();
                /*设置表标题*/
                this.title='编辑';
                /*清空列表*/
                this.addForm={//双向绑定的对象
                    	id:null,
                        name:'',
                        sn:'',
                        state:0,
                        parent:null,
                        employee:null
                };
                /*显示模态框*/
                this.editFormVisible = true;
			},
			//新增编辑
			addSubmit(){
			    /*检验表单规则*/
			    this.$refs.addForm.validate((valid)=>{
			        if(valid){// 符合规则
			            this.$confirm('确认提交吗？','提示',{}).then(()=>{
			                /*加载效果开启*/
			                this.addLoading = true;
			                /*拷贝对象*/
                            let para=Object.assign({}, this.addForm);
                            if(para.parent){ //防止用户没有选择上级部门
                                let pid =para.parent[para.parent.length-1];
                                para.parent={
                                    id:pid
                                }
                            }
                            this.$http.put("/dept",para).then(res=>{
                                if(res.data.success){
                                    this.$message({
                                        type: 'success',
                                        message: res.data.message
                                    });
                                    //刷新数据
                                    this.loadListData();
                                }else {
                                    this.$message({
                                        type: 'error',
                                        message: res.data.message
                                    });
                                }
                            })
                            //关闭加载效果
                            this.addLoading=false;
                            //关闭模态框
                            this.editFormVisible=false;
						})
					}
				})
			},
			selsChange(sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove() {
			    /*获取选中的id*/
                var ids = this.sels.map(item => item.id);
				/*删除警告*/
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
				    /*打开警告框*/
					this.listLoading = true;
					this.$http.patch("/dept",ids).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
					})
						this.loadListData();
				}).catch(() => {

				})
			},
            /*加载部门的级联下拉框*/
            loadListDeptTree(){
                this.$http.get("/dept/deptTrees").then(res=>{
                    this.deptTree=res.data;
                    console.debug(this.deptTree)
                })
            },
			/*抽屉数据查询*/
            handleDrawer(i,row){
                if(row){
                    /*获取该项id*/
                    this.deptId = row.id
                }
                let para = {
                    ecurrentPage: this.ecurrentPage,
                    epageSize:3,
                    keyname: this.empfilters.keyname,
                    id: this.deptId,
                }
                /*打开抽屉*/
                this.drawer = true;
                /*发送请求获取数据*/
				this.$http.post("/dept/emp",para).then(res=>{
                    this.gridData = res.data.bags;
                    this.etotal=res.data.total;
				}).catch(()=>{
                    this.$message({
                        message: '系统繁忙，请稍后重试!',
                        type: 'error'
                    });
                })
			},
			/*抽屉高级查询*/
            queryDrawer(){
                ecurrentPage: this.ecurrentPage=1;
                this.handleDrawer();
			},
			/*部门经理*/
            loadEmps(){
                this.$http.get("/emp").then(res=>{
                    this.emps=res.data;
                })
            },
			/*抽屉新增*/
            empAdd(i, row){
                /*设置表标题*/
                this.etitle='编辑';
                /*清空列表*/
                this.eaddForm={
                    id:null,
                    username:'',
                    email:'',
                    phone:'',
                    password:'',
                    age:'',
                    state:0,
                    department_id:''
                };
                this.eeditFormVisible=true;
			},
            /*编辑*/
            empEdit(i, row){
                /*设置表标题*/
                this.etitle='编辑';
                /*打开抽屉模态框*/
                this.eeditFormVisible=true;
                /*设置回显*/
                let para=Object.assign(this.eaddForm, row);
			},
			/*抽屉批量删除*/
            empRemove(){
                /*获取选中的id*/
                var ids = this.sels.map(item => item.id);
                /*删除警告*/
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    /*打开警告框*/
                    this.listLoading = true;
                    this.$http.patch("/emp",ids).then(res=>{
                        this.listLoading = false;
                        if(res.data.success){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }else{
                            this.$message({
                                message: res.data.msg,
                                type: 'error'
                            });
                        }
                    })
                    this.handleDrawer();
                }).catch(() => {

                })
			},
			/*抽屉分页*/
            eCurrentChange(page){
                this.ecurrentPage=page;
                this.handleDrawer();
			},
            /*删除*/
            empDel(i, row) {
                this.$confirm('亲，确认删除数据?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    let id = row.id;
                    this.$http.delete("/emp/"+id).then(res=>{
                        if(res.data.success){
                            this.$message({
                                type:'success',
                                message:res.data.message
                            });
                        }else {
                            this.$message({
                                type: 'error',
                                message: res.data.message
                            });
                        }
                        // 刷新列表
                        this.handleDrawer();
                    })
                }).catch(()=>{
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                })

            },
            /*新增编辑方法*/
            eaddSubmit(){
                /*检验表单规则*/
                this.$refs.eaddForm.validate((valid)=>{
                    if(valid){// 符合规则
                        this.$confirm('确认提交吗？','提示',{}).then(()=>{
                            /*加载效果开启*/
                            this.eaddLoading = true;
                            /*拷贝对象*/
                            let para=Object.assign({}, this.eaddForm);
                            para.department_id=this.deptId;
                            this.$http.put("/emp",para).then(res=>{
                                if(res.data.success){
                                    this.$message({
                                        type: 'success',
                                        message: res.data.message
                                    });
                                    //刷新数据
                                    this.handleDrawer();
                                }else {
                                    this.$message({
                                        type: 'error',
                                        message: res.data.message
                                    });
                                }
                            })
                            //关闭加载效果
                            this.eaddLoading=false;
                            //关闭模态框
                            this.eeditFormVisible=false;
                        })
                    }
                })
            },
		},
		mounted() {
			this.loadListData();
			this.loadEmps();
		}
	}

</script>

<style scoped>

</style>